<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>

</html>
<!-- <script src="./01_finalizationRegistry.js"></script> -->


<script>
  // ES12: WeakRef类
  // WeakRef.prototype.deref: 
  // > 如果原对象没有销毁, 那么可以获取到原对象
  // > 如果原对象已经销毁, 那么获取到的是undefined
  const finalRegistry = new FinalizationRegistry((value) => {
    console.log("注册在finalRegistry的对象, 某一个被销毁", value)
  })

  let obj = { name: "why" }
  let info = new WeakRef(obj)

  finalRegistry.register(obj, "obj")

  obj = null

  setTimeout(() => {
    console.log(info.deref()?.name)
    console.log(info.deref() && info.deref().name)
  }, 10000)


</script>